﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using QuanLiThuVien.Source;
using QuanLiThuVien.LinkModel;

namespace QuanLiThuVien
{
    /// <summary>
    /// Interaction logic for SachHong.xaml
    /// </summary>
    public partial class SachHong : Page
    {
        private InsertInfo m_insertInfo = new InsertInfo();
        private UpdateInfo m_updateInfo = new UpdateInfo();
        private DeleteInfo m_deleteInfo = new DeleteInfo();
        private ShowInfo m_showInfo = new ShowInfo();
        private XuLyThem m_XuLyKhac = new XuLyThem();

        static bool m_bSelectData = false;

        private THUVIEN.Service1 serv = new THUVIEN.Service1();
        public SachHong()
        {
            InitializeComponent();
            m_showInfo.ListSachMatHong(dataSachHong, 2);
        }

        private void dataSachHong_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!m_bSelectData)
            {
                var item = dataSachHong.SelectedItem as DSSach;
                txtMaCuonSach_SH.Text = item.MaSach.ToString();
                txtTenSach_SH.Text = item.TenDauSach.ToString();
                txtNhaXuatBan_SH.Text = item.NhaXuatBan.ToString();
                txtNamXuatBan_SH.Text = Int32.Parse(item.NamXuatBan.ToString()).ToString();
                txtTriGia_SH.Text = item.DonGia.ToString();
                txtTheLoai_SH.Text = item.TenTheLoai.ToString();
                txtTacGia_SH.Text = item.HoTenTacGia.ToString();
            }
            else
            {
                m_bSelectData = false;
                m_showInfo.ListSachMatHong(dataSachHong, 2);
            }
        }

        private void bntThemSachHong_Click(object sender, RoutedEventArgs e)
        {
            if (serv.SelectTinhTrangCuonSach(txtMaCuonSach_SH.Text) == -1)
                MessageBox.Show("Kiểm tra lại mã cuốn sách", "Thông Báo");
            else if (serv.SelectTinhTrangCuonSach(txtMaCuonSach_SH.Text) == 1)
                MessageBox.Show("Cuốn sách này đang có người mượn", "Thông Báo");
            else if (serv.SelectTinhTrangCuonSach(txtMaCuonSach_SH.Text) == 3)
                MessageBox.Show("Cuốn sách này đã bị mất", "Thông Báo");
            else
            {
                m_updateInfo.UpdateTinhTrangCuonSach(txtMaCuonSach_SH.Text, 2);
                string m_sMaSach = serv.SelectMaSach_CuonSach(txtMaCuonSach_SH.Text);
                int m_iSoLuongSach = serv.SelectSoLuong_Sach(m_sMaSach);
                m_updateInfo.UpdateSoLuong_Sach(m_sMaSach, m_iSoLuongSach - 1);
            }
        }
    }
}
